import store from "../store/index.js"; //vuex  
// let server_url='http://massage.weixiaoba.vip/technician';//请求地址
// let server_url='http://moushilang.hdhzyl.cn/technician';//请求地址
// let server_url='https://www.baidu.shop/technician';//请求地址
let server_url = 'https://pw.zhijiayitong.com.cn'; //请求地址
// http://admin.gegedaojia.com/#/admin/Home/index.html
let token = '';
process.env.NODE_ENV === 'development' ? '192.168.0.1' : '';

function service(options = {}) {
	store.state.token && (token = store.state.token); //从vuex中获取登录凭证
	/* let token ;
	   if(store.state.token ) {
		   token = store.state.token 
	   }else{
		   uni.getStorageSync({
		   	    key: 'token',
		   	    success: function (res) {
		   			console.log(res)
		   	        token = res.data
		   	    }
		   })
	   } */

	let arr = uni.getStorageSync('token')
	console.log(token.token, '*********////');
	console.log(arr.token, '*/////***////');

	options.url = `${server_url}${options.url}`;
	//配置请求头
	options.header = {
		'content-type': 'application/x-www-form-urlencoded',
		'technician-token': `${arr.token}` //Bearer 
	};

	return new Promise((resolved, rejected) => {
		//成功
		options.success = (res) => {

			if (Number(res.data.code) == 200) { //请求成功
				resolved(res.data, res.code, res.msg);
			} else {
				if (res.data.msg == '请先登录') { //请求成功
					uni.removeStorageSync("token");
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 3000)

					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: `${res.data.msg}`
					});
					rejected(res.data); //错误
				} else {
					uni.showToast({
						icon: 'none',
						duration: 3000,
						title: `${res.data.msg}`
					});
					rejected(res.data); //错误
				}
			}

		}
		//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);

	});
}
export default service;